<template>
    <div class="user-management-container">

        <!-- 右侧用户管理 -->
        <div class="right-panel">
            <el-form :inline="true" :model="dataps" class="search-form" v-if="isshows">
                <el-form-item label="编码">
                    <el-input v-model="dataps.VendorCord" clearable />
                </el-form-item>
                <el-form-item label="名称架构">
                    <el-input v-model="dataps.FactoryName" clearable />
                </el-form-item>
                <el-form-item label="创建人">
                    <el-input v-model="dataps.CreateName" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="GetFactory">查询</el-button>
                </el-form-item>
            </el-form>

            <!-- 搜索表单 -->


            <!-- 操作按钮区域 -->
            <div style="margin-left: 1200px;">
                <el-button type="success" @click="GetFactory()" disabled>刷新缓存</el-button>
                <el-icon style="margin-left: 10px; cursor: pointer;">
                    <ZoomOut @click="show" />
                </el-icon>
                <el-icon style="margin-left: 10px; cursor: pointer;">
                    <Refresh @click="GetFactory()" />
                </el-icon>
            </div>
            <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" border default-expand-all>
                <el-table-column type="index" label="序号" sortable />
                <el-table-column prop="vendorCord" label="编码" sortable />
                <el-table-column prop="factoryName" label="名称" sortable />
                <el-table-column prop="isCust" label="是否自定义" sortable>
                    <template v-slot="scoped">
                        {{ scoped.row.isCust ? "是" : "否" }}
                    </template>
                </el-table-column>
                <el-table-column prop="servers" label="数据库类型" sortable />
                <el-table-column prop="maessage" label="备注" sortable />
                <el-table-column prop="createName" label="创建人" sortable />
                <el-table-column prop="createTime" label="创建时间" sortable>
                    <template v-slot="scoped">
                        {{ moment(scoped.row.createTime).format("YYYY-MM-DD") }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" sortable>
                    <template v-slot="scoped">
                        <el-button type="primary" @click="CreateRole(scoped.row)">编辑</el-button>
                        <el-button type=" success" @click="DelOrcan(scoped.row.factoryId)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="pagination-container">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="page.pageIndex" :page-sizes="[2, 4, 6, 8]" :page-size="page.pageSize"
                    layout="total, sizes, prev, pager, next, jumper" :total="page.totalCount">
                </el-pagination>
            </div>
        </div>
    </div>

    <el-dialog v-model="dialogFormVisible" :title="models.title" width="500">
        <el-form :model="forms">
            <el-form-item label="编码">
                <el-input v-model="forms.vendorCord" autocomplete="off" />
            </el-form-item>
            <el-form-item label="公司名称">
                <el-input v-model="forms.factoryName" autocomplete="off" />
            </el-form-item>
            <el-form-item label="是否自定义">
                <el-radio-group v-model="forms.isCust">
                    <el-radio :value="true" size="large">是</el-radio>
                    <el-radio :value="false" size="large">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="数据库类型">
                <el-select v-model="forms.servers">
                    <el-option label="MySql" value="MySql" />
                    <el-option label="SqlServer" value="SqlServer" />
                </el-select>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="forms.maessage" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>

                <el-button type="primary" @click="CreateRoles">
                    更新
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue';
import { APIs } from '../../../Http/http'
import { useCounterStore } from '../../../stores/counter'
import { EditPen, Delete, ZoomOut, Refresh } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import moment, { max } from 'moment';
const store = useCounterStore();
const page = ref({
    pageIndex: 1,
    pageSize: 10,
    total: 0,
    totalCount: 0
})

const isshows = ref(false)
const show = () => {

    if (isshows.value == false) {
        isshows.value = true;

    }
    else {
        isshows.value = false;

    }
}
const dataps = ref({
    VendorCord: "",
    FactoryName: "",
    CreateName: ""
})

const tableData = ref<any>([
    {
        "factoryId": 1,
        "vendorCord": "100",
        "factoryName": "苏州xxx有限公司",
        "isCust": false,
        "servers": "Mysql",
        "maessage": "很好的共厂",
        "createName": "管理员",
        "createTime": "2025-09-04T20:29:37",
        "updateName": null,
        "updateTime": null,
        "deleteName": null,
        "deleteTime": null,
        "isDel": false
    }
])

const handleChange = (value: any) => {

    console.log(value)
}

const GetFactory = () => {
    APIs.get("/api/Read/FatoryQuery", {
        params: {
            pageIndex: page.value.pageIndex,
            pageSize: page.value.pageSize,
            total: page.value.total,
            totalCount: page.value.totalCount,
            VendorCord: dataps.value.VendorCord,
            FactoryName: dataps.value.FactoryName,
            CreateName: dataps.value.CreateName,
        }
    }).then(res => {
        tableData.value = res.data.data.data;
        page.value.total = res.data.data.total;
        page.value.totalCount = res.data.data.totalCount;
    })
}

const DelOrcan = (mid: number) => {
    ElMessageBox.confirm(
        '确定要删除吗?',
        '警告',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            APIs.delete("/api/Write/FactoryDel", { data: { id: mid } }).then(res => {
                if (res.data.code == 100) {
                    ElMessage.success("删除成功");
                    GetFactory();
                }
                else {
                    ElMessage.error("删除成功")
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: 'Delete canceled',
            })
        })

}

const forms = ref({
    "createName": "string",
    "createTime": "2025-09-08T02:25:43.329Z",
    "updateName": "string",
    "updateTime": "2025-09-08T02:25:43.329Z",
    "deleteName": "string",
    "deleteTime": "2025-09-08T02:25:43.329Z",
    "isDel": true,
    "factoryId": 0,
    "vendorCord": "string",
    "factoryName": "string",
    "isCust": true,
    "servers": "string",
    "maessage": "string"
})

const dialogFormVisible = ref(false)
const models = ref({
    title: "",
    isAdd: false
})
const CreateRole = (account: any) => {

    dialogFormVisible.value = true
    forms.value.factoryId = account.factoryId
    forms.value.vendorCord = account.vendorCord
    forms.value.factoryName = account.factoryName
    forms.value.isCust = account.isCust
    forms.value.servers = account.servers
    forms.value.maessage = account.maessage
    forms.value.createName = account.createName
    forms.value.createTime = account.createTime
    forms.value.updateName = account.updateName
    forms.value.updateTime = account.updateTime
    forms.value.deleteName = account.deleteName
    forms.value.deleteTime = account.deleteTime
    forms.value.isDel = account.isDel
    models.value.title = "更新组织"
    models.value.isAdd = false


}



const CreateRoles = () => {


    APIs.put("/api/Write/FactoryUpdate", { data: forms.value }).then(res => {
        if (res.data.code == 100) {
            ElMessage.success("更新成功")
            GetFactory();
            dialogFormVisible.value = false
        }
        else {
            ElMessage.error("更新失败")
            dialogFormVisible.value = false
        }
    })
}








// 分页处理函数
const handleSizeChange = (val: number) => {
    page.value.pageSize = val;
    GetFactory();
}

const handleCurrentChange = (val: number) => {
    page.value.pageIndex = val;

    GetFactory();
}

onMounted(() => {
    GetFactory();
})

</script>